import asyncio
import random
from pyppeteer import launch
from fake_useragent import UserAgent
from bs4 import BeautifulSoup

# 请替换为你的实际账号和密码
USERNAME = '账号'
PASSWORD = '密码'
browser_instances = []


async def openBrowser(url):
    browser = await launch(
        headless=False,
        args=['--disable-infobars', '--window-size=1366,768', '--no-sandbox']
    )
    browser_instances.append(browser)

    # 设置用户代理
    page = await browser.newPage()
    user_agent = UserAgent().random
    await page.setUserAgent(user_agent)

    # 转到主网站
    width, height = 1366, 768
    await page.setViewport({'width': width, 'height': height})

    # 禁用 headless 模式检测
    await page.evaluateOnNewDocument(
        '''() =>{ Object.defineProperties(navigator, { webdriver: { get: () => false } }) }'''
    )

    await page.goto(url)
    await asyncio.sleep(random.uniform(2, 3))
    return browser, page


async def main():
    url = 'https://www.jiuyangongshe.com/'
    browser, page = await openBrowser(url)

    try:
        # 2. 点右上角“登录注册按钮”
        await page.waitForXPath('//div[@class="name lf" and contains(text(), "登录注册")]')
        login_register_buttons = await page.xpath('//div[@class="name lf" and contains(text(), "登录注册")]')
        if login_register_buttons:
            await login_register_buttons[0].click()

        # 3. 点击账号密码登录
        await page.waitForSelector('#tab-accounts')
        await page.click('#tab-accounts')
        await asyncio.sleep(2)
        # 4. 输入账号密码，点击登录
        phone_input = await page.querySelector('input[name="phone"]')
        await page.keyboard.press('Tab')
        await asyncio.sleep(1)
        await phone_input.type(USERNAME)
        password_input = await page.querySelector('input[name="password"]')
        await password_input.click()
        await asyncio.sleep(1)
        await password_input.type(PASSWORD)

        print("登录中...")
        await page.waitForXPath('//button[@class="el-button el-button--primary"]/span[contains(text(), "登录")]')
        login_buttons = await page.xpath(
            '//button[@class="el-button el-button--primary"]/span[contains(text(), "登录")]')
        if login_buttons:
            await login_buttons[0].click()

        # 等待登录成功，可根据实际情况调整等待时间或使用更精确的等待方式
        await asyncio.sleep(5)
        print("登录成功...")

        # 5. 点顶部TAB“异动”
        await page.waitForXPath('//div[@class="header-box parent"]/div[@class="li"]/div[contains(text(), "异动")]')
        abnormal_tab = await page.xpath(
            '//div[@class="header-box parent"]/div[@class="li"]/div[contains(text(), "异动")]')
        if abnormal_tab:
            await abnormal_tab[0].click()
        # 等待页面加载
        await asyncio.sleep(5)

        # 6. 点TAB“全部异动解析”
        # 使用 XPath 定位到 yd-tabs_item is-top 的 div 里的第一个 div
        await page.waitForXPath('//div[@class="yd-tabs_item is-top"]/div[1]')
        # 获取定位到的元素
        all_abnormal_analysis = await page.xpath('//div[@class="yd-tabs_item is-top"]/div[1]')
        if all_abnormal_analysis:
            # 点击该元素
            await all_abnormal_analysis[0].click()
        # 等待页面加载
        await asyncio.sleep(5)

        # 7 获取该页面Html
        html = await page.content()
        # print(html)

        keywords, keyword_detail_list = parse_html(html)
        print(keywords)
        print(keyword_detail_list)

    except Exception as e:
        print(f"爬取失败: {e}")
    finally:
        # 关闭浏览器
        for browser in browser_instances:
            await browser.close()


def parse_html(html_content):
    soup = BeautifulSoup(html_content, 'html.parser')

    # 第一个对象的大集合
    keyword_detail_list = []

    # 第二个对象的集合，用于存储所有关键字
    keywords = []

    # 查找所有的li元素，每个li元素可能包含一个关键字及其相关股票信息
    all_li_elements = soup.find_all('li', class_='module')

    for li in all_li_elements:
        # 查找关键字
        keyword_element = li.find('div', class_='fs18-bold lf')
        keyword_number_element = li.find('div', class_='number lf')
        if keyword_element:
            keyword = keyword_element.text.strip()
            keyword_number = keyword_number_element.text.strip()
            if keyword == '公告':
                continue
            keywords.append(keyword)  # 添加关键字到第二个对象集合


            # 查找股票信息
            stock_info_list = []
            stock_items = li.find_all('li', class_='row drvi straight-line')
            for stock_item in stock_items:
                stock_name_element = stock_item.find('div', class_='shrink fs15-bold')
                if stock_name_element:
                    stock_name = stock_name_element.text.strip()

                    # 查找解析说明
                    expound_element = stock_item.find('pre', class_='pre tl hilll')
                    if expound_element:
                        expound_text = expound_element.text.strip()
                        stock_info = {
                            "stock_name": stock_name,
                            "expound_text": expound_text
                        }
                        stock_info_list.append(stock_info)

            # 组装第一个对象的数据
            keyword_detail_list.append({
                "keyword": keyword,
                "keyword_number": keyword_number,
                "stock_info_list": stock_info_list
            })

    return keywords,keyword_detail_list


# 示例调用
if __name__ == "__main__":
    asyncio.run(main())
